﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Atosenet
{
    public abstract class TimingExecutor
    {
        #region 成员

        private System.Threading.Timer _timer;
        private TimerCallback _tickEvent;
        private int _checkInterval = 1000;

        private bool _enable = false, _threading = false;

        #endregion


        #region 初始化

        public TimingExecutor(int checkInterval)
        {
            _checkInterval = checkInterval;
            _enable = false; 
            Init();
        }

        public TimingExecutor(int checkInterval, bool initialState)
        {
            _checkInterval = checkInterval;
            _enable = initialState;
            Init();            
        }

        public TimingExecutor()
        {
            _enable = false; 
            Init();
        }

        private void Init()
        {
            Init_CallbackMethod();
            Init_Timer();
        }

        private void Init_Timer()
        {
            if (_enable)
            {
                _timer = new System.Threading.Timer(_tickEvent, null, 0, _checkInterval);
            }
            else
            {
                _timer = new System.Threading.Timer(_tickEvent, null, Timeout.Infinite, Timeout.Infinite);
            }
        }

        private void Init_CallbackMethod()
        {
            _tickEvent = new TimerCallback(TimerTickEvent);
        }

        #endregion


        #region 公开函数

        public void StartTimer()
        {
            _timer.Change(0, _checkInterval);
        }

        public void StopTimer()
        {
            _timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
        }

        public void ResetTimer()
        {
            StopTimer();
            StartTimer();
        }

        public void Execute()
        {
            WorkThreadMethod();
        }

        #endregion


        #region 核心函数

        private void TimerTickEvent(object state)
        {
            StartWorkThread();
        }

        private void StartWorkThread()
        {
            if (_threading)
            {
                Thread work_thread = new Thread(new ThreadStart(WorkThreadMethod));
                work_thread.IsBackground = true;
                work_thread.Start();
            }
            else
            {
                WorkThreadMethod();
            }
        }

        protected abstract void WorkThreadMethod();

        #endregion


        #region 属性

        public bool Enable
        {
            get { return _enable;}
            set
            {
                if (_enable != value)
                {
                    _enable = value;

                    if (_enable)
                        StartTimer();
                    else
                        StopTimer();
                }
            }
        }

        public int CheckInterval
        {
            get { return _checkInterval; }
            set { _checkInterval = value; }
        }

        public bool IsThreadingProc
        {
            get { return _threading; }
            set { _threading = value; }
        }

        #endregion
    }
}
